package command2;

import command2.command.Command;
import command2.command.MacroCommand;
import command2.drawer.DrawCanvas;
import command2.drawer.DrawCommand;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Main extends JFrame implements ActionListener, MouseMotionListener {
    //绘制的历史记录
    private MacroCommand history=new MacroCommand();
    //绘制区域
    private DrawCanvas canvas=new DrawCanvas(400,400,history);
    //删除按钮
    private JButton clearButton=new JButton("clear");

    public Main(String title) {
//        this.history = history;
        super(title);

//        this.addWindowListener( this);
        canvas.addMouseMotionListener(this);
        clearButton.addActionListener(this);

        Box buttonBox=new Box(BoxLayout.X_AXIS);
        buttonBox.add(clearButton);
        Box mainBox=new Box(BoxLayout.Y_AXIS);
        mainBox.add(buttonBox);
        mainBox.add(canvas);

        getContentPane().add(mainBox);

        pack();
        show();
    }

    public static void main(String[] args) {
        new Main("Command pattern Sample");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==clearButton){
            history.clear();
            canvas.repaint();
        }
    }

    @Override
    public void mouseDragged(MouseEvent e) {

        Command cmd=new DrawCommand(canvas,e.getPoint());
        history.append(cmd);
        cmd.execute();

    }

    @Override
    public void mouseMoved(MouseEvent e) {


    }


}
